using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Models.Repositories
{
    #region using
    using TemplateWeb.Helpers;
    using TemplateWeb.Models.ViewModels;
    using TemplateWeb.Models;
    #endregion
    public class SachRepository
    {
        public List<Sach> SelectBestSeller()
        {
            try
            {
                List<Sach> lst = new List<Sach>();
                foreach (Sach sach in DataContext.Instance.Sach)
                {
                    if (sach.HangMuc.Equals(StringEnum.GetStringValue(HangMucSach.BEST_SELLER)))
                    {
                        lst.Add(sach);
                    }
                }
                lst = lst.OrderByDescending(n => n.NgayXuatBan).ToList();
                return lst;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

        public List<Sach> SelectAll()
        {
            try
            {
                return DataContext.Instance.Sach.OrderByDescending(n => n.NgayXuatBan).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

        public Sach SelectByID(int id)
        {
            try
            {
                return DataContext.Instance.Sach.FirstOrDefault(item => item.ID == id);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return null;
            }
        }

        public List<Sach> SelectPaging(int page, int pageSize)
        {
            try
            {
                var lstItem = DataContext.Instance.Sach.OrderByDescending(n => n.NgayXuatBan).ToList();
                if (pageSize > 0)
                {
                    int startIndex, itemCount;
                    startIndex = (page - 1) * pageSize;
                    itemCount = pageSize;
                    if (startIndex + itemCount > lstItem.Count)
                        itemCount = lstItem.Count - startIndex;
                    lstItem = lstItem.GetRange(startIndex, itemCount);
                }
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - SelectPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - SelectPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

        public bool Insert(Sach obj)
        {
            try
            {
                DataContext.Instance.Sach.AddObject(obj);
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                var delitem = DataContext.Instance.Sach.FirstOrDefault(item => item.ID == id);
                DataContext.Instance.Sach.DeleteObject(delitem);
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return false;
            }
        }

        public bool Save()
        {
            try
            {
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public int GetTotalItem()
        {
            try
            {
                return DataContext.Instance.Sach.Count();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return -1;
            }
        }

        public int GetTotalItemByIDLoaiSach(int idloaisach)
        {
            try
            {
                return (from p in DataContext.Instance.Sach where p.IDLoaiSach == idloaisach select p).Count();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - GetTotalItemByIDLoaiSach: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - GetTotalItemByIDLoaiSach: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return -1;
            }
        }


        public int GetTotalPage()
        {
            int RecordCount = GetTotalItem();
            int PageSize = WebConfiguration.NumSachPerPage;
            return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
        }

        public int GetTotalPageByIDLoaiSach(int idloaisach)
        {
            try
            {
                int RecordCount = GetTotalItemByIDLoaiSach(idloaisach);
                int PageSize = WebConfiguration.NumSachPerPage;
                return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - GetTotalPageByIDLoaiSach: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - GetTotalPageByIDLoaiSach: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return -1;
            }
        }


        public List<Sach> RetrieveByID(int id)
        {
            try
            {
                return (from item in DataContext.Instance.Sach where item.ID == id select item).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

        public List<Sach> SelectByIDLoaiSach(int idloaisach)
        {
            try
            {
                var lstItem = (from item in DataContext.Instance.Sach where item.IDLoaiSach == idloaisach select item).OrderByDescending(n => n.NgayXuatBan).ToList();
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - SelectByIDLoaiSach: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - SelectByIDLoaiSach: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

        public List<Sach> SelectByIDLoaiSachPaging(int idloaisach, int page, int pageSize)
        {
            try
            {
                var lstItem = (from item in DataContext.Instance.Sach where item.IDLoaiSach == idloaisach select item).OrderByDescending(n => n.NgayXuatBan).ToList();
                if (pageSize > 0)
                {
                    int startIndex, itemCount;
                    startIndex = (page - 1) * pageSize;
                    itemCount = pageSize;
                    if (startIndex + itemCount > lstItem.Count)
                        itemCount = lstItem.Count - startIndex;
                    lstItem = lstItem.GetRange(startIndex, itemCount);
                }
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - SelectByIDLoaiSachPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - SelectByIDLoaiSachPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

        public List<Sach> Search(string tukhoa, string tacgia)
        {
            try
            {
                var lst = new List<Sach>();
                foreach (var item in DataContext.Instance.Sach)
                {
                    if (item.SanPham.TenSanPham.ToUpper().Contains(tukhoa.ToUpper()) &&
                        item.TacGia.ToUpper().Contains(tacgia == string.Empty ? item.TacGia.ToUpper() : tacgia.ToUpper()))
                    {
                        lst.Add(item);
                    }
                }
                var lstItem = lst.OrderByDescending(n => n.NgayXuatBan).ToList();
                return lstItem;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("Sach - Search: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("Sach - Search: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<Sach>();
            }
        }

    }
}